package Forms;

import Classes.MInterface;
import Classes.Student;
import Classes.User;
import java.awt.Color;
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.awt.print.PrinterException;
import java.io.File;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.text.MessageFormat;
import java.util.ArrayList;
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.table.DefaultTableModel;

public class ProfilePic extends javax.swing.JFrame {
    static MInterface mi;
    static Student stud;
    static File file;
    String port;
    
    /** Creates new f orm AdminForm_1 */
    public ProfilePic(MInterface i, Student s) {
        
        initComponents();
        this.setLocationRelativeTo(getRootPane());
        mi = i;
        stud = s;
        filename.setVisible(false);
        if(s != null) {
            try {
//                ImageIcon pic = (ImageIcon) mi.getPic(photoL, stud.getStuID());
//                photoL.setIcon(pic);
            } catch(Exception e) {
                e.printStackTrace();
            }
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        cancelB = new javax.swing.JButton();
        photoL = new javax.swing.JLabel();
        cancelB1 = new javax.swing.JButton();
        filename = new javax.swing.JLabel();
        saveB = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle("Upload Profile Picture");

        cancelB.setFont(new java.awt.Font("Tahoma", 0, 12));
        cancelB.setText("Cancel");
        cancelB.setName("cancelB"); // NOI18N
        cancelB.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                cancelBMouseClicked(evt);
            }
        });

        photoL.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        photoL.setName("photoL"); // NOI18N

        cancelB1.setFont(new java.awt.Font("Tahoma", 0, 12));
        cancelB1.setText("Upload");
        cancelB1.setName("cancelB1"); // NOI18N
        cancelB1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                cancelB1MouseClicked(evt);
            }
        });
        cancelB1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelB1ActionPerformed(evt);
            }
        });

        filename.setFont(new java.awt.Font("Tahoma", 1, 14));
        filename.setText("No File Selected");
        filename.setName("filename"); // NOI18N

        saveB.setFont(new java.awt.Font("Tahoma", 0, 12));
        saveB.setText("Save");
        saveB.setName("saveB"); // NOI18N
        saveB.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                saveBMouseClicked(evt);
            }
        });
        saveB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveBActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 278, Short.MAX_VALUE)
                        .addComponent(saveB)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cancelB)
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(photoL, javax.swing.GroupLayout.PREFERRED_SIZE, 412, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(cancelB1)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(filename)))
                        .addGap(0, 8, Short.MAX_VALUE))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(6, 6, 6)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cancelB1, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(filename))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(photoL, javax.swing.GroupLayout.PREFERRED_SIZE, 335, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cancelB, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(saveB, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void cancelBMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_cancelBMouseClicked
        this.setVisible(false);
    }//GEN-LAST:event_cancelBMouseClicked

    private void cancelB1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_cancelB1MouseClicked
        // TODO add your handling code here:
    }//GEN-LAST:event_cancelB1MouseClicked

    private void cancelB1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelB1ActionPerformed
        JFileChooser fc = new JFileChooser();
        fc.addChoosableFileFilter(new FileNameExtensionFilter("JPEG Images Only", "jpg"));
        fc.setAcceptAllFileFilterUsed(false);
        int res = fc.showOpenDialog(this);
        file = fc.getSelectedFile();
        filename.setVisible(true);
        if(res == JFileChooser.APPROVE_OPTION) {
            try {
                photoL.setIcon(new ImageIcon(ImageIO.read(file).getScaledInstance(photoL.getWidth(), photoL.getHeight(), Image.SCALE_SMOOTH)));
                filename.setText(file.getName());
            } catch(Exception e) {
                e.printStackTrace();
            }
        } else {
            filename.setText("No File Selected");
        }   
    }//GEN-LAST:event_cancelB1ActionPerformed

    private void saveBMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_saveBMouseClicked
        // TODO add your handling code here:
    }//GEN-LAST:event_saveBMouseClicked

    private void saveBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveBActionPerformed
        try {
//            boolean res = mi.managePic(file, Integer.valueOf(stud.getStuID()));
//            if(res) {
//                JOptionPane.showMessageDialog(null, "Photo Saved!", "", JOptionPane.INFORMATION_MESSAGE);
//            }
        } catch(Exception e) {
            e.printStackTrace();
        }
    }//GEN-LAST:event_saveBActionPerformed

    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new ProfilePic(mi, stud).setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cancelB;
    private javax.swing.JButton cancelB1;
    private javax.swing.JLabel filename;
    private javax.swing.JLabel photoL;
    private javax.swing.JButton saveB;
    // End of variables declaration//GEN-END:variables
}
